<?php session_start(); //Session ?>
<?php include_once( "_db.php" ); //Database ?>
<?php include_once( "funcdef.php" ); //Functions Definition ?>
<?php include_once( "header.php" ); //Header ?>
<?php include_once( "footer.php" ); //Footer ?>
<?php
//include page header
header_template("Bag", $_SERVER['PHP_SELF'], "icon-briefcase");

//db OBJECT invoked!
$db = new DATABASE();
?>
      <div class="row">
        <div class="span6">
<?php
if(!isset($_SESSION['shoppingBag'])) {
  $_SESSION['shoppingBag'] = array();
}

sort($_SESSION['shoppingBag']);
$bagUnique = array_unique($_SESSION['shoppingBag']);
$bagCount = array_count_values($_SESSION['shoppingBag']);
/*
echo "<pre>";
echo print_r($bagProd);
echo "</pre>";
echo "<pre>";
echo print_r($_SESSION['shoppingBag']);
echo print_r($bagUnique);
echo print_r($bagCount);
echo "</pre>";
*/
$err = bag_operation($db, $_GET["action"], $_GET["pid"]);
if ($err != "") {
  printAlert("error", "Oh snap!", $err, $_SERVER['PHP_SELF']);
}
sort($_SESSION['shoppingBag']);
$bagUnique = array_unique($_SESSION['shoppingBag']);
$bagCount = array_count_values($_SESSION['shoppingBag']);
/*
  echo "<pre>";
  echo print_r($bagProd);
  echo "</pre>";
  echo "<pre>";
  echo print_r($_SESSION['shoppingBag']);
  echo print_r($bagUnique);
  echo print_r($bagCount);
  echo "</pre>";
*/
$bagProd = array();
$bagProd = bag_construction($db);
sort($_SESSION['shoppingBag']);
$bagUnique = array_unique($_SESSION['shoppingBag']);
$bagCount = array_count_values($_SESSION['shoppingBag']);
/*
echo "<pre>";
echo print_r($bagProd);
echo "</pre>";
echo "<pre>";
echo print_r($_SESSION['shoppingBag']);
echo print_r($bagUnique);
echo print_r($bagCount);
echo "</pre>";
*/
if (isset($_GET["mode"]) && ($_GET["mode"] == "simple")) {
  $data_content  = "\n";
  $data_content .= "<table class='table table-condensed'>\n";
  $data_content .= "<tbody>\n";

  foreach($bagProd as $value) {
    if (in_array($value['product_id'], $bagUnique)) {
      $bagPrice[$value['product_id']] = $bagCount[$value['product_id']] * $value['product_price'];
      if ($value['picture_name'] != "") {
        $img_src_resize = "timthumb.php?src=".$value['picture_name']."&w=60&h=45";
      } else {
        $img_src_resize = "http://placehold.it/60x45";
      }
        $data_content .= "<tr>\n";
        $data_content .= "<td><img src='".$img_src_resize."'></td>\n";
        $data_content .= "<td>\n";
        $data_content .= "<span class='label label-default'>MYR ".$value['product_price']."</span>\n";
        $data_content .= "<span class='label label-info'>x".$bagCount[$value['product_id']]."</span>\n";
        //$data_content .= "<br />\n";
        $data_content .= "<span class='label label-success'>MYR ".sprintf("%01.2f", ($value['product_price']*$bagCount[$value['product_id']]))."</span>\n";
        $data_content .= "</td>\n";
        $data_content .= "</tr>\n";
    }
  }
  $data_content .= "<tr>\n";
  $data_content .= "<td>&nbsp;</td>\n";
  $data_content .= "<td>\n";
  $data_content .= "<span class='label label-inverse'>TOTAL</span>&nbsp;";
  if (isset($bagPrice)) {
    $totPrice = array_sum($bagPrice);
  } else {
    $totPrice = 0;
  }
  $data_content .= "<span class='label label-success'>MYR ".sprintf("%01.2f", $totPrice)."</span>";
  $data_content .= "</td>\n";
  $data_content .= "</tr>\n";
  $data_content .= "</tbody>\n";
  $data_content .= "</table>\n";
  $totItem = count($bagUnique);
?>
          <div class="pull-right">
            <a href="<?php echo $_SERVER['PHP_SELF']; ?>" rel="popover" title="Bag" data-content="<?php echo $data_content; ?>">
              <img src="pic/glyphicons/png/glyphicons_350_shopping_bag.png"> <?php echo $totItem; ?> item(s) in bag
            </a>
          </div>
<?php
} else {
?>
          <table class="table table-condensed table-striped">
            <thead>
              <tr>
                <th>Product</th>
                <th style="text-align: center;">Quantity</th>
                <th style="text-align: center;">Action</t>
                <th style="text-align: right;">Unit Price</th>
                <th style="text-align: right;">Price</th>
              </tr>
            </thead>
            <tbody>
<?php
  foreach($bagProd as $value) {
    if (in_array($value['product_id'], $bagUnique)) {
      $bagPrice[$value['product_id']] = $bagCount[$value['product_id']] * $value['product_price'];
      if (($bagCount[$value['product_id']]+1) <= $value['product_quantity']) {
        $inc_btn_href  = "href=\"".$_SERVER['PHP_SELF']."?action=inc&pid=".$value['product_id']."\"";
        $inc_btn_state = "";
        $inc_btn_title = "Increase";
      } else {
        $inc_btn_href  = "";
        $inc_btn_state = " disabled";
        $inc_btn_title = "Reached maximum";
      }
      if (($bagCount[$value['product_id']]-1) > 0) {
        $dec_btn_href  = "href=\"".$_SERVER['PHP_SELF']."?action=dec&pid=".$value['product_id']."\"";
        $dec_btn_state = "";
        $dec_btn_title = "Decrease";
      } else {
        $dec_btn_href  = "";
        $dec_btn_state = " disabled";
        $dec_btn_title = "Reached minimum";
      }
      $rem_btn_href  = "href=\"".$_SERVER['PHP_SELF']."?action=rem&pid=".$value['product_id']."\"";
      $rem_btn_state = "";
      $rem_btn_title = "Remove";
?>
              <tr>
                <td><?php echo $value['product_name']; ?></td>
                <td style="text-align: center;"><?php echo $bagCount[$value['product_id']]."/".$value['product_quantity']; ?></td>
                <td style="text-align: center;">
                  <a <?php echo $inc_btn_href; ?> rel="tooltip" title="<?php echo $inc_btn_title; ?>" class="btn btn-mini btn-info<?php echo $inc_btn_state; ?>">&#43;</a>
                  <a <?php echo $dec_btn_href; ?> rel="tooltip" title="<?php echo $dec_btn_title; ?>" class="btn btn-mini btn-info<?php echo $dec_btn_state; ?>">&#45;</a>
                  <a <?php echo $rem_btn_href; ?> rel="tooltip" title="<?php echo $rem_btn_title; ?>" class="btn btn-mini btn-danger<?php echo $rem_btn_state; ?>">&#215;</a>
                  <!--<a <?php echo $inc_btn_href; ?> rel="tooltip" title="<?php echo $inc_btn_title; ?>"><span class="label label-default"><i class="icon-plus <?php echo $inc_btn_icn; ?>"></i></span></a>-->
                  <!--<a <?php echo $dec_btn_href; ?> rel="tooltip" title="<?php echo $dec_btn_title; ?>"><span class="label label-default"><i class="icon-minus <?php echo $dec_btn_icn; ?>"></i></span></a>-->
                  <!--<a <?php echo $rem_btn_href; ?> rel="tooltip" title="<?php echo $rem_btn_title; ?>"><span class="label label-important"><i class="icon-remove <?php echo $rem_btn_icn; ?>"></i></span></a>-->
                </td>
                <td style="text-align: right;"><?php printf("%01.2f", $value['product_price']); ?></td>
                <td style="text-align: right;"><?php printf("%01.2f", $bagPrice[$value['product_id']]); ?></td>
              </tr>
<?php
    }
  }
?>
              <tr>
                <td colspan="4"><h4>TOTAL</h4></td>
<?php
  if (isset($bagPrice)) {
    $totPrice = array_sum($bagPrice);
  } else {
    $totPrice = 0;
  }
?>
                <td style="text-align: right;"><?php printf("%01.2f", $totPrice); ?></td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
<?php
}
//include page footer
footer_template();
?>
